<?php
class Customer extends CI_Controller{
	
	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -  
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
	
	function index(){
		$this->load->model('Customer_management');
		$data['customers'] = $this->Customer_management->getCustomers();
		$data['customerCode'] = $this->Customer_management->generateCustomerCode();
		$data['siteCode'] = $this->Customer_management->generateSiteCode();
		//$data['main_content'] = 'create_customer';
		$data['main_content'] = 'customer_management';
		$this->load->view('layouts/general', $data);
	}
	
	
	
	function project(){
		$data['main_content']='project';
		$this->load->view('layouts/general', $data);
		}
	
	function newCustomer(){
		$this->load->model('Customer_management');
		$data['customers'] = $this->Customer_management->getCustomers();
		$data['customerCode'] = $this->Customer_management->generateCustomerCode();
		$data['siteCode'] = $this->Customer_management->generateSiteCode();
		$data['main_content'] = 'create_customer';		
		$this->load->view('layouts/general', $data);
	}


	function searchCustomer(){
		$this->load->model('Customer_management');
		$data['customers'] = $this->Customer_management->getCustomer($this->input->post('customerCode'));
		$data['main_content'] = 'customer_management';
		$this->load->view('layouts/general', $data);
	}
	function updateCustomer(){
		$this->load->model('Customer_management');
		$data['customers'] = $this->Customer_management->getCustomer($this->input->post('customerCode'));
		$data['branches'] = $this->Customer_management->getBranches($this->input->post('customerCode'));
		$data['hq'] = $this->Customer_management->getHQ($this->input->post('customerCode'));
		$data['main_content'] = 'customer_update';
		$this->load->view('layouts/general', $data);
	}
	function update(){
		$customer_data = array(
			'customerCode' => $_POST['customerCode'],
			'firstname' => $_POST['firstname'],
			'lastname' => $_POST['lastname'],
			'email' => $_POST['email'],
			'dateUpdated' => strtotime("now")
			);
		$this->load->model('Customer_management');
		$data['success_message'] = '';
		if($this->Customer_management->updateCustomer($customer_data)){
			$data['success_message'] .= 'You have successfully updated a customer!';
		}else{
			$data['success_message'] .= 'Something went wrong while updating the customer!';	
		}
		$data['main_content'] = 'success_message';
		$this->load->view('layouts/general',$data);
		
	}

	function create(){
		$sort = explode('CS',$_POST['siteCode']);
		$customer_data = array(
			'customerCode' => $_POST['customerCode'],
			'firstname' => $_POST['firstname'],
			'lastname' => $_POST['lastname'],
			'email' => $_POST['email'],
			'dateCreated' => strtotime("now")
		);
		$site_data = array(
		'siteCode' => $_POST['siteCode'],
		'name' => $_POST['siteName'],
		'address' => $_POST['siteAddress'],
		'postcode' => $_POST['sitePostcode'],
		'customerCode' => $_POST['customerCode'],
		'sort' => $sort[1],
		'type' => 'head'
		);
		$this->load->model('Customer_management');
		$data['success_message'] = '';
		if($this->Customer_management->createCustomer($customer_data)){
			if($this->Customer_management->createSite($site_data)){
				
			}else{
				$data['success_message'] .= 'Something went wrong while creating the site!';	
			}
			
			$data['success_message'] .= 'You have created a new customer!';
		}else{
			$data['success_message'] .= 'Something went wrong while creating the customer!';	
		}
		$data['main_content'] = 'success_message';
		$this->load->view('layouts/general',$data);

	}
		
	
}
?>